<?php
namespace Cms\Controller;
use Think\Controller;
class ApiController extends Controller {

	public static $WAP;//CMS全局静态变量
	public $AJAX_TYPE = '';
	
	protected function _initialize(){
		if(isset($_GET[C('VAR_JSONP_HANDLER')])) $this->AJAX_TYPE = 'JSONP';
		$this->log();
	}
	
	//开锁
	public function unlock(){
		$result = array('status'=>0);
		$code = I("dev_code");
		$from = I("from");
		if(!$code||!in_array($from, array('hardware','app'))){
			$result['msg'] = '输入参数错误';
			$this->ajaxReturn($result, $this->AJAX_TYPE);
		}
		if($from=='hardware'){
			
		}else{
			
		}
		$result['status'] = 1;
		$result['msg'] = '操作成功';	
		$result['data'] = I("param.");
		$this->ajaxReturn($result, $this->AJAX_TYPE);
	}
	
	/**
	 * @package 接口日志
	 * @param userid:用户标识
	 * @param send_data:发送数据
	 * @param return_data:接口调用结果
	 * @param remark:操作描述（如操作内容等）
	 */
	public function log(){
		$event = MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME;
		$data=I('param.');
		$remark = json_encode($data);
		$adddata = array('event'=>$event, 'create_date'=>date("Y-m-d H:i:s"), 'clientip'=>get_client_ip());
		if($data['userid']) $adddata['userid'] = $data['userid'];
		if($data['send_data']) $adddata['send_data'] = is_array($data['send_data'])?json_encode($data['send_data']):$data['send_data'];
		if($data['return_data']) $adddata['return_data'] = is_array($data['return_data'])?json_encode($data['return_data']):$data['return_data'];
		if($data['remark']) $adddata['remark'] = $data['remark'];
		else $adddata['remark'] = $remark;
		$result = array('status'=>0);
		$res = M("event_log")->add($adddata);
		return $res;
	}
	
	public function send_unlock(){
		error_reporting(1);
		$code = "openlock";
		$lockno = "100002";//"0xAA 0xBB 0xCC 0xDD 0XEE 0xFF";
		$url = "http://localhost:8800/?opcode=$code&lockno=$lockno";//121.41.109.134
		$str = file_get_contents($url);
		file_put_contents(date("Ymd").".log",date("Y-m-d H:i:s").": ".$str."\r\n",FILE_APPEND);
		echo $str;
	}

	/*****************用户登录API**********************/
 /**
     * 用户登录
     * @param $username string 用户名
     * @param $password string 用户密码
     * @return mixed
     * @throws Exception
     */

	public function login($username, $password){
	    if (empty($username) || empty($password)) {
            //throw new Exception("用户名不能为空", Error::USERNAME_CONNOT_NULL);
           $info['status'] = 2;
           $info['msg'] = '用户名或者密码不能为空!';
        }else{
	        $password = md5($password);
	        $res=M('driver')->where(array('username'=>$username,'password'=>$password))->find();
	        if($res){
	           $info['status'] = 0;
	           $info['msg'] = '登录成功!';
	        }else{
	           $info['status'] = 1;
	           $info['msg'] = '用户名或者密码错误!';
	        }
        }

       $this->ajaxReturn($info, $this->AJAX_TYPE);
    }

	
	function httppost($url,$curlPost){	  	
	  	$curl = curl_init();	  	
	  	curl_setopt($curl, CURLOPT_POST, 1);
    	curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
//		      'Content-Type: application/json; charset=utf-8',
//		      'Content-Length: ' . strlen($curlPost)
//		    )
//		);
	  	$return_str = curl_exec($curl);
	  	$error = curl_error($curl);
	  	curl_close($curl);
	  	if($error) return $error;
	  	return $return_str;
  }
	
}